﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

public partial class Main_AjaUtils_ReminderPopup : System.Web.UI.Page
{
    /*page defaults*/
    OfficeOnlineUser currentUser;

    private void AuthenticateUser()
    {
        try
        {
            currentUser = (OfficeOnlineUser)Session["CurrentUser"];
        }
        catch (Exception ex)
        {
            Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }

        if (currentUser == null)
        {
            Response.Redirect("../login.aspx?urlReferrer=" + Request.Url);
        }
    }
    /*end page defaults*/
  

    protected void Page_Load(object sender, EventArgs e)
    {
        AuthenticateUser();
        String action = Request.QueryString["action"];
        if (action == "stopreminder")
        {
            int result = StopReminder();
            switch (result)
            {
                case 1:
                    frmReminderPopup.InnerHtml = "<response>Success</response>";
                    break;
                default:
                    frmReminderPopup.InnerHtml = "<response>Fail</response>";
                    break;
            }
        }
        else if (action == "postponereminder")
        {
            int result = PostponeReminder();
            switch (result)
            {
                case 1:
                    frmReminderPopup.InnerHtml = "<response>Success</response>";
                    break;
                default:
                    frmReminderPopup.InnerHtml = "<response>Fail</response>";
                    break;
            }
        }
        else
            BindRemindersTable();
    }

    private void BindRemindersTable()
    {
        string command = "sp_tb_user_GetUserRemindersForHiddenGrid @userID='" + currentUser.UserName + "'";
        SqlConnection sqlCon = new SqlConnection(DatabaseFunctions.GetConnectionString());
        sqlCon.Open();
        SqlDataAdapter sda = new SqlDataAdapter(command, sqlCon);

        DataSet dsReminders = new DataSet();
        sda.Fill(dsReminders);

        sqlCon.Close();

        hiddenReminderGrid.DataSource = dsReminders;
        hiddenReminderGrid.DataBind();
    }

    private int StopReminder()
    {
        Reminder rmdr = new Reminder();
        rmdr.ID = Request.QueryString["reminderID"];
        return rmdr.Stop();
    }

    private int PostponeReminder()
    {
        Reminder rmdr = new Reminder();        
        int min = Convert.ToInt32(Request.QueryString["min"]) + 5;
        rmdr.AlarmTime = Request.QueryString["hour"] + ":" + min.ToString() + ":00 " + Request.QueryString["AmPm"];
        rmdr.ID = Request.QueryString["reminderID"];
        return rmdr.Postpone();
    }
}
